var api = require('../../../config/api.js');
var util = require('../../../utils/util.js');
var app = getApp()

// pages/ucenter/mobile/mobile.js
Page({
  data: {
    mobile: '',
    avatarUrl: '',
    nickName: '',
    userName: '',
    disableGetMobileCode: false,
    disableSubmitMobileCode: true,
    getCodeButtonText: '获取验证码'
  },

  onShow: function () { },

  onLoad: function () {
    var that = this
    var userInfo = wx.getStorageSync('userInfo');
    that.setData({
      avatarUrl: userInfo.avatarUrl,
      nickName: userInfo.nickName,
      userName: userInfo.userName
    })

    if (app.globalData.token) { } else {
      var token = wx.getStorageSync('userToken')
      if (token) {
        app.globalData.token = token
      }
    }

  },

  bindCheckMobile: function (mobile) {
    if (!mobile) {
      wx.showModal({
        title: '错误',
        content: '请输入手机号码'
      });
      return false
    }
    if (!mobile.match(/^1[3-9][0-9]\d{8}$/)) {
      wx.showModal({
        title: '错误',
        content: '手机号格式不正确，仅支持国内手机号码'
      });
      return false
    }
    return true
  },

  bindGetPassCode: function (e) {
    var that = this
    that.setData({
      disableGetMobileCode: true
    })
  },

  bindInputMobile: function (e) {
    this.setData({
      mobile: e.detail.value,
    })
  },

  countDownPassCode: function () {
    if (!this.bindCheckMobile(this.data.mobile)) {
      return
    }
    wx.request({
      url: api.SmsCode,
      data: {
        mobile: this.data.mobile
      },
      method: 'GET',
      success: function (res) {
        console.log(res);
        if (res.data.status === 1) {
          wx.showToast({
            title: '发送成功',
            icon: 'success',
            duration: 1000
          })
          var pages = getCurrentPages()
          var i = 60;
          var intervalId = setInterval(function () {
            i--
            if (i <= 0) {
              pages[pages.length - 1].setData({
                disableGetMobileCode: false,
                disableSubmitMobileCode: false,
                getCodeButtonText: '获取验证码'
              })
              clearInterval(intervalId)
            } else {
              pages[pages.length - 1].setData({
                getCodeButtonText: i,
                disableGetMobileCode: true,
                disableSubmitMobileCode: false
              })
            }
          }, 1000);
        }
      }
    })
  },

  bindLoginMobilecode: function (e) {
    var that = this;
    var mobile = this.data.mobile;
    if (!this.bindCheckMobile(mobile)) {
      return
    }
    if (!(e.detail.value.code && e.detail.value.code.length === 6)) {
      return
    }
    wx.showToast({
      title: '操作中...',
      icon: 'loading',
      duration: 5000
    })

    wx.request({
      url: api.bindUserName,
      data: {
        username: that.data.userName,        
        mobile_code: e.detail.value.code,
        mobile: mobile
      },
      method: 'GET',
      success: function (res) {
        var path = res.data.path
        console.log(res);
        if (res.data.status === 1) {
          wx.hideToast();
        //清除用户信息，重新登录
          wx.setStorageSync('userInfo', '');
          wx.setStorageSync('token', '');
          wx.setStorageSync('user_id', '');
          wx.setStorageSync('inviter_info', '');

          wx.showModal({
            title: res.data.msg,
            content: res.data.content,
            showCancel: false,
            success: function(res) {
              wx.switchTab({
                url: path
              });
            }
          })
        } else {
          wx.showModal({
            title: '提示',
            content: '验证码错误',
            showCancel: false
          })
        }
      }
    })
  }
})